/**
 * 
 */
package com.dhcc.android.base.utils;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 文件处理集合
 * 
 * @author pengling
 * @date 2015-7-13
 */
public class FileUtil {
	public static boolean deleteFile(String path) {
		System.out.println("delete:" + path);
		boolean flag = true;
		try {
			File file = new File(path);
			if (file != null) {
				if (file.exists()) {
					flag = file.delete();
				}
			}
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	public static String getSavePath() {
		String path = Environment.getExternalStorageDirectory() + "/surveyPlus";
		// String path = "/storage/sdcard1/test";
		return path;
	}

	public static String getTime(File f) {
		long time = f.lastModified();
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		String s = cal.getTime().toLocaleString();
		return s;
	}

	public static File createFile(String path) {
		File mPhotoFile = new File(path);
		if (!mPhotoFile.exists()) {
			try {
				if (path.endsWith("/")) {// 文件夹
					mPhotoFile.mkdirs(); // 如果父目录不存在，就新建一个
				} else {
					if (!mPhotoFile.getParentFile().exists()) {
						mPhotoFile.getParentFile().mkdirs(); // 如果父目录不存在，就新建一个
					}
					mPhotoFile.createNewFile();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return mPhotoFile;
	}

	/**
	 * 追加文件：使用FileWriter
	 * 
	 * @param fileName
	 * @param content
	 */
	public static void writeFile(String fileName, String content) {
		FileWriter writer = null;
		try {
			// 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
			writer = new FileWriter(fileName, true);
			writer.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
					System.gc();
					writer = null;
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 给文件命名
	 * 
	 * @param after
	 *            后缀名
	 * @return
	 */
	public static String getPhotoFileName(String after) {

		Date date = new Date(System.currentTimeMillis());
		String name = "'IMG'_yyyyMMdd_HHmmss";
		if (after.contains("mp3")) {
			name = name.replace("IMG", "VOICE");
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(name);
		return dateFormat.format(date) + after;// ".jpg"
	}

	/**
	 * 重命名文件夹
	 * 
	 * @param path
	 *            路径
	 * @param newName
	 *            新名字
	 * @return
	 */
	public static Boolean renameFile(String path, String newPath) {
		File f = new File(path);
		if (f.exists()) {
			File newFile = new File(newPath);
			return f.renameTo(newFile);
		}
		return false;

	}

	/**
	 * 文件是否存在
	 * 
	 * @param path
	 * @return
	 */
	public static boolean isExistFile(String path) {
		File mPhotoFile = new File(path);
		return mPhotoFile.exists();
	}

	/**
	 * 复制单个文件
	 * 
	 * @param oldPath
	 *            String 原文件路径 如：c:/fqf.txt
	 * @param newPath
	 *            String 复制后路径 如：f:/fqf.txt
	 * @return boolean
	 */
	public static void copyFile(String oldPath, String newPath) {
		try {
			int byteread = 0;
			File oldfile = new File(oldPath);
			File newfile = new File(newPath);
			System.out.println("-----复制文件");
			if (!oldfile.exists()) {
				createFile(oldPath);
				copyFile(oldPath, newPath);
			} else if (!newfile.exists()) {
				createFile(newPath);
				copyFile(oldPath, newPath);
			} else {
				InputStream inStream = new FileInputStream(oldPath); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e) {
			System.out.println("复制单个文件操作出错");
			e.printStackTrace();
		}

	}

	public static void saveFile(String fileName, String content) {
		File file = new File(fileName);
		if (!file.exists()) {
			try {
				if (fileName.endsWith("/")) {// 文件夹
					file.mkdirs(); // 如果父目录不存在，就新建一个
				} else {
					if (!file.getParentFile().exists()) {
						file.getParentFile().mkdirs(); // 如果父目录不存在，就新建一个
					}
					file.createNewFile();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		FileWriter writer = null;
		try {
			// 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
			writer = new FileWriter(fileName, true);
			writer.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
					System.gc();
					writer = null;
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
